package be.kdg.netpresentadmin;

import java.io.IOException;
import java.rmi.*;
import java.rmi.registry.*;

import javax.swing.*;

import be.kdg.netpresent.*;

// Main Class as starting point for a user friendly admin-client GUI.
// The main method creates an instance of this class which in turn
// handles the rest.
public class Main {
	
	public static void main(String[] args) {
		new Main();
	}

	public Main() {
		try {
			// For the ease of using and testing the application, a popup at the
			// beginning will ask for the IP of the server. This can be "localhost"
			// for example if testing it locally.
			String sServerIP = getServerIPfromUser();

			// After obtaining the address, build up the RMI connection.
			NetPresentInt serverObject = (NetPresentInt) LocateRegistry
					.getRegistry(sServerIP, 2021).lookup("/NetPresent");
			
			// Create the first "real" popup which will ask for either running
			// or creating a presentation. Of course, just cancelling is possible too.
			AdminFirstChoice afcInst0 = new AdminFirstChoice(serverObject);

			// Keep popping up the above GUI element, until the user quits through cancel.
			while (afcInst0.waitForChoice());
			
			// Debug prints, normal and within/coupled with possible exceptions.
			System.out.println("Exit please!");

		} catch (RemoteException re) {
			System.out.println();
			System.out.println("In Main: RemoteException");
			System.out.println(re);
		} catch (NotBoundException nbe) {
			System.out.println();
			System.out.println("In Main: NotBoundException");
			System.out.println(nbe);
		} catch (java.lang.ArithmeticException ae) {
			System.out.println();
			System.out.println("In Main: ArithmeticException");
			System.out.println(ae);
		} catch (IOException ioe) {
			System.out.println();
			System.out.println("In Main: IOException");
			System.out.println(ioe);
		} catch (InterruptedException ie) {
			System.out.println();
			System.out.println("In Main: InterruptedException");
			System.out.println(ie);
		}
	}

	// Method mentioned above to get some ease to use the application,
	// especially while developing. This way, if switched between local
	// and network addresses, code has not to be changed.
	private static String getServerIPfromUser() {
		// Voluntary try-catch to catch NullPointerExceptions due to canceling
		// the input-dialog as a whole.
		String sServerIP = null;

		// Some GUI-make-up...
		try {
			sServerIP = JOptionPane.showInputDialog(null,
					("Geef het IP-adres van de server "
							+ "om NetPresent te starten"),
					"NetPresent - Admin Connect", 1);

			while (sServerIP.length() == 0) {
				sServerIP = JOptionPane.showInputDialog(null,
						("Error: IP kan niet leeg zijn!\n"
								+ "Geef het IP-adres van de server"
								+ "om de presentaties te laden:"),
						"NetPresent - Admin Connect", 1);
			}
		} catch (NullPointerException e) {
			System.out.println("IP input gecancelled");
			return null;
		}

		return sServerIP;
	}
}
